 package com.zx.deploy.common.utils;
 
 import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
 public class DBUtil
 {
	 
	private String dbPath;

	public DBUtil(String dbPath) {
		this.dbPath = dbPath;
	}
   
   public boolean do_update(String sql)
     throws Exception
   {
     Class.forName("org.sqlite.JDBC");
     Connection conn = DriverManager.getConnection("jdbc:sqlite:" + dbPath);
 
     Statement stat = conn.createStatement();
     stat.executeUpdate(sql);
 
     conn.close();
     return true;
   }
 
   public boolean do_update2(String sql)
     throws Exception
   {
     String dbPath = super.getClass().getResource("/").getPath();
     String os = System.getProperty("os.name");
     if ((os.toLowerCase().startsWith("win")) && 
       (dbPath.startsWith("/"))) {
       dbPath = dbPath.substring(1);
       dbPath = dbPath.replace("/", "\\");
     }
 
     dbPath = dbPath + "servlet-context.xml";
 
     Class.forName("org.sqlite.JDBC");
     Connection conn = DriverManager.getConnection("jdbc:sqlite:" + dbPath);
 
     Statement stat = conn.createStatement();
     stat.executeUpdate(sql);
 
     conn.close();
     return true;
   }
 
   public List executeQuery(String sql)
   {
     SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
     List rslist = new ArrayList();
     StringBuffer sqlPage = new StringBuffer(sql + " ");
     Connection conn = null;
     PreparedStatement pstmt = null;
     ResultSet rs = null;
     try
     {
 
       Class.forName("org.sqlite.JDBC");
       conn = DriverManager.getConnection("jdbc:sqlite:" + dbPath);
       pstmt = conn.prepareStatement(sqlPage.toString());
       rs = pstmt.executeQuery();
       ResultSetMetaData rsmd = rs.getMetaData();
       int numberOfColumns = rsmd.getColumnCount();
       while (rs.next()) {
         Map row = new HashMap(numberOfColumns);
         for (int i = 1; i <= numberOfColumns; ++i) {
           Object o = rs.getObject(i);
           if (("Date".equalsIgnoreCase(rsmd.getColumnTypeName(i))) && 
             (o != null))
             row.put(rsmd.getColumnName(i), formatter.format(o));
           else {
             row.put(rsmd.getColumnName(i), (o == null) ? "" : o);
           }
         }
         rslist.add(row);
       }
     }
     catch (Exception e)
     {
       try {
         rs.close();
         pstmt.close();
         conn.close();
       }
       catch (SQLException localSQLException)
       {
       }
     }
     finally {
       try {
         rs.close();
         pstmt.close();
         conn.close();
       }
       catch (SQLException localSQLException2) {
       }
     }
     return rslist;
   }
 
   public List executeQuery2(String sql)
   {
     SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
     List rslist = new ArrayList();
     StringBuffer sqlPage = new StringBuffer(sql + " ");
     Connection conn = null;
     PreparedStatement pstmt = null;
     ResultSet rs = null;
     try
     {
       String dbPath = super.getClass().getResource("/").getPath();
 
       String os = System.getProperty("os.name");
       if ((os.toLowerCase().startsWith("win")) && 
         (dbPath.startsWith("/"))) {
         dbPath = dbPath.substring(1);
         dbPath = dbPath.replace("/", "\\");
       }
 
       dbPath = dbPath.replace("%20", " ");
 
       dbPath = dbPath + "servlet-context.xml";
 
       Class.forName("org.sqlite.JDBC");
       conn = DriverManager.getConnection("jdbc:sqlite:" + dbPath);
 
       pstmt = conn.prepareStatement(sqlPage.toString());
       rs = pstmt.executeQuery();
       ResultSetMetaData rsmd = rs.getMetaData();
       int numberOfColumns = rsmd.getColumnCount();
       while (rs.next()) {
         Map row = new HashMap(numberOfColumns);
         for (int i = 1; i <= numberOfColumns; ++i) {
           Object o = rs.getObject(i);
           if (("Date".equalsIgnoreCase(rsmd.getColumnTypeName(i))) && 
             (o != null))
             row.put(rsmd.getColumnName(i), formatter.format(o));
           else {
             row.put(rsmd.getColumnName(i), (o == null) ? "" : o);
           }
         }
         rslist.add(row);
       }
     } catch (Exception e) {
       System.out.println("系统启动时，取路径出错，程序布署路径不能有空隔！");
       System.out.println(e.getMessage());
       try {
         rs.close();
         pstmt.close();
         conn.close();
       } catch (SQLException localSQLException) {
       }
     }
     finally {
       try {
         rs.close();
         pstmt.close();
         conn.close();
       } catch (SQLException localSQLException2) {
       }
     }
     return rslist;
   }
 
   public int executeQueryForCount(String sql)
   {
     Connection conn = null;
     Statement stmt = null;
     ResultSet rs = null;
     int rowCount = 0;
     try {
       Class.forName("org.sqlite.JDBC");
       conn = DriverManager.getConnection("jdbc:sqlite:" + dbPath);
 
       PreparedStatement pstmt = conn.prepareStatement(sql);
       rs = pstmt.executeQuery();
 
       while (rs.next()) {
         ++rowCount;
       }
     }
     catch (Exception e)
     {
       System.out.println(e.getMessage());
       try
       {
         rs.close();
 
         conn.close();
       }
       catch (SQLException localSQLException)
       {
       }
     }
     finally {
       try {
         rs.close();
 
         conn.close();
       }
       catch (SQLException localSQLException2) {
       }
     }
     return rowCount;
   }
 
   public Object setinsertData(String sql)
     throws Exception
   {
     Class.forName("org.sqlite.JDBC");
     Connection conn = DriverManager.getConnection("jdbc:sqlite:" + dbPath);
 
     Statement stmt = null;
     String flagOper = "0";
     try
     {
       stmt = conn.createStatement();
       return Integer.valueOf(stmt.executeUpdate(sql));
     }
     catch (SQLException e)
     {
       throw new Exception(e.getMessage());
     }
     finally {
       try {
         stmt.close();
         conn.close();
       } catch (SQLException e) {
         throw new Exception(e.getMessage());
       }
     }
   }


 }

